/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package windowComponents;

import data.email.Attachment;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

/**
 *
 * @author Arcanis
 */
public class AttachmentLabel extends JLabel {

    public AttachmentLabel(final Attachment att) {
        super();
        if (att.getFileName().length() > 20) {
            setText(" " + att.getFileName().substring(0, 17) + "... ");
        } else {
            setText(" " + att.getFileName() + " ");
        }
        setToolTipText(att.getFileName());
        setBorder(new LineBorder(Color.black));

        setCursor(new Cursor(Cursor.HAND_CURSOR));

        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    JFileChooser fc = new JFileChooser("./");
                    fc.setSelectedFile(new File(att.getFileName()));
                    int returnVal = fc.showSaveDialog(labelFor);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        att.Download(fc.getSelectedFile().getParent()+File.separator, fc.getSelectedFile().getName(), true);
                    }
                } catch (Exception ex) {
                    Logger.getLogger(AttachmentLabel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}
